# -*- coding: utf-8 -*- 
#!/usr/bin/python2.7
"""MVC的cotroler,url映射后会生成对象，并且调用GET或者POST返回"""

from core.renderers import MakoRenderer
from core.handlers import RequestHandler
from core.services.DataService import DataServiceFactory
from core.services.FormService import FormServiceFactory
from pages import PageFactory
import web


class Redirect(RequestHandler):
    """去掉url后的'/'直接跳转。"""
    def GET(self, path):
        web.seeother('/' + path)
 
class ReadThemeFile(RequestHandler):
    def GET(self, filePath,other):
        return DataServiceFactory.FileServiceInstance().get('themes/'+filePath)

class ReadComponentFile(RequestHandler):
    def GET(self, filePath,other):
        return DataServiceFactory.FileServiceInstance().get('statics/components/'+filePath)

class ViewIndexPage(RequestHandler):
    def GET(self):
        pageNum=int(web.input(page="1").page)
        page=PageFactory.IndexPageInstance()
        renderer=MakoRenderer()
        if pageNum==0 or pageNum>page.getMaxPageNum(): 
            page=PageFactory.NofoundPageInstance()
            return renderer.render(page.getTemplateAbsPath(),page)
        else:
            page.turnToPage(pageNum)
            return renderer.render(page.getTemplateAbsPath(),page)
class ViewArticlePage(RequestHandler):
    def __init__(self):
        self.dataService=DataServiceFactory.ArticleServiceInstance()
    def GET(self,link):
        link=u'/'+link
        renderer=MakoRenderer()
        if self.dataService.exist(link):
            page=PageFactory.ArticlePageInstance(link)
            return renderer.render(page.getTemplateAbsPath(),page) 
        else:
            page=  PageFactory.NofoundPageInstance()
            return renderer.render(page.getTemplateAbsPath(),page)

     
class ViewCategoryArchivePage(RequestHandler):
    def __init__(self):
        self.dataService=DataServiceFactory.CategroyServiceInstance()
    def GET(self,categoryName):
        renderer=MakoRenderer()
        if self.dataService.exist(categoryName):#判断是否有此分类
            page=PageFactory.CategoryArchivePageInstance(categoryName)
            pageNum=int(web.input(page="1").page)
            if pageNum==0 or pageNum>page.getMaxPageNum(): #页码值是否正确
                page=PageFactory.NofoundPageInstance()
            else:
                page.turnToPage(pageNum)
        else:
            page=PageFactory.NofoundPageInstance()
        return renderer.render(page.getTemplateAbsPath(),page)
class ViewDateArchivePage(RequestHandler):
    def GET(self,year,month):
        year=int(year)
        month=int(month)
        renderer=MakoRenderer()
        if DataServiceFactory.ArchiveServiceInstance().exist(year,month):#判断是否有此时间
            page=PageFactory.DateArchivePageInstance(year,month)
            pageNum=int(web.input(page="1").page)
            if pageNum==0 or pageNum>page.getMaxPageNum(): #页码值是否正确
                page=PageFactory.NofoundPageInstance()
            else:
                page.turnToPage(pageNum)
        else:
            page=PageFactory.NofoundPageInstance()
        return renderer.render(page.getTemplateAbsPath(),page)
class ViewTagArchivePage(RequestHandler):
    def GET(self,tagName):
        renderer=MakoRenderer()
        if DataServiceFactory.TagServiceInstance().exist(tagName):#判断是否有此标签
            page=PageFactory.TagArchivePageInstance(tagName)
            pageNum=int(web.input(page="1").page)
            if pageNum==0 or pageNum>page.getMaxPageNum(): #页码值是否正确
                page=  PageFactory.NofoundPageInstance()
            else:
                page.turnToPage(pageNum)
        else:
            page=PageFactory.NofoundPageInstance()
        return renderer.render(page.getTemplateAbsPath(),page)
    
class ViewFeedPage(RequestHandler):
    def GET(self):
        web.header("Content-Type", "application/xml")
        renderer=MakoRenderer()
        page=PageFactory.FeedPageInstance()
        return renderer.render(page.getTemplateAbsPath(),page)  
class ViewNofoundPage(RequestHandler):
    def GET(self,url):
        renderer=MakoRenderer()
        page=PageFactory.NofoundPageInstance()
        return renderer.render(page.getTemplateAbsPath(),page)
    
class AddArticleComment(RequestHandler):
    def GET(self,permalink):
        web.seeother('/' + permalink)
    def POST(self,permalink):
        permalink=u'/'+permalink
        commentFormService=FormServiceFactory.CommentNewFormServiceInstance()
        commentForm=commentFormService.get()
        if not commentForm.validates():
            page=PageFactory.ArticlePageInstance(permalink)
            page.commentForm=commentForm
            renderer=MakoRenderer()
            return renderer.render(page.getTemplateAbsPath(),page)
            #web.seeother('/' + permalink)
        else:
            comment=commentFormService.submit()
            web.seeother(permalink+'#comment-'+ '%d' % comment.id())
        